package top.fengyulou.xiaofeng.controller.admin;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.fengyulou.xiaofeng.common.Jr;
import top.fengyulou.xiaofeng.dto.ClassifyDto;
import top.fengyulou.xiaofeng.dto.ExamDto;
import top.fengyulou.xiaofeng.dto.PaperDto;
import top.fengyulou.xiaofeng.dto.UserDto;
import top.fengyulou.xiaofeng.service.ClassifyService;
import top.fengyulou.xiaofeng.service.MaterialService;
import top.fengyulou.xiaofeng.service.PaperService;
import top.fengyulou.xiaofeng.service.UserService;

import javax.annotation.Resource;

/**
 * 公共内容
 *
 * @Author guochao
 * @Date 2022/2/8 21:56
 * @Version 1.0
 */
@Controller
public class AdminCommonController {
    @Resource
    private MaterialService materialService;
    @Resource
    private PaperService paperService;
    @Resource
    private UserService userService;
    @Resource
    private ClassifyService classifyService;

    //选择图片
    @RequestMapping("/admin/select/image")
    public String selectImage(Model model) {
        model.addAttribute("list", materialService.list(null));
        return "admin/admin-select-image";
    }

    //选择试卷
    @RequestMapping("/admin/select/paper")
    public String selectPaper(Model model, String noDataIds) {
        model.addAttribute("noDataIds", noDataIds);
        return "admin/admin-select-paper";
    }

    //试卷列表
    @RequestMapping("/admin/select/ajax/paper")
    @ResponseBody
    public Jr ajaxPaper(@RequestBody PaperDto paperDto) {
        paperDto.setNoDataIdsArr(ObjectUtils.isNotEmpty(paperDto.getNoDataIds())?paperDto.getNoDataIds().split(","):null);
        paperService.list(paperDto);
        return Jr.yesd(paperDto);
    }

    //选择用户
    @RequestMapping("/admin/select/user")
    public String selectUser(Model model, String noDataIds) {
        model.addAttribute("noDataIds", noDataIds);
        return "admin/admin-select-user";
    }

    //用户列表
    @RequestMapping("/admin/select/ajax/user")
    @ResponseBody
    public Jr ajaxUser(@RequestBody UserDto user) {
        user.setNoDataIdsArr(ObjectUtils.isNotEmpty(user.getNoDataIds())?user.getNoDataIds().split(","):null);
        userService.list(user);
        return Jr.yesd(user);
    }

    //选择分类
    @RequestMapping("/admin/select/classify")
    public String selectClassify(Model model, String noDataIds) {
        model.addAttribute("noDataIds", noDataIds);
        return "admin/admin-select-classify";
    }

    //分类列表
    @RequestMapping("/admin/select/ajax/classify")
    @ResponseBody
    public Jr ajaxClassify(@RequestBody ClassifyDto classifyDto) {
        classifyDto.setNoDataIdsArr(ObjectUtils.isNotEmpty(classifyDto.getNoDataIds()) ? classifyDto.getNoDataIds().split(",") : null);
        classifyService.listPage(classifyDto);
        return Jr.yesd(classifyDto);
    }
}
